package com.jinkaimen.goodcash.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.igexin.sdk.PushManager;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.AccountShopBean;
import com.jinkaimen.goodcash.bean.BalanceBean;
import com.jinkaimen.goodcash.bean.HomeOpreratorBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.service.GetuiService;
import com.jinkaimen.goodcash.ui.activity.AccountActivity;
import com.jinkaimen.goodcash.ui.activity.IdentificationShopStep1Activity;
import com.jinkaimen.goodcash.ui.activity.LoginActivity;
import com.jinkaimen.goodcash.ui.activity.ShopManagerActivity;
import com.jinkaimen.goodcash.ui.activity.TransactionHistoryActivity;
import com.jinkaimen.goodcash.ui.activity.WebViewActivity;
import com.jinkaimen.goodcash.ui.adapter.HomeOperatorAdapter;
import com.jinkaimen.goodcash.ui.dialog.TipsDialog;
import com.jinkaimen.goodcash.utils.AppUtils;
import com.jinkaimen.goodcash.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;

public class HomeFragment extends BaseFragment{

    private GridView mGridView;
    private List<HomeOpreratorBean> mDataList;
    private HomeOperatorAdapter mHomeOperatorAdapter;
    private TextView mBalanceView;
    private TipsDialog mTipsDialog;
    private View mLeaderContainner;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected View initContentView() {
        return UIUtils.inflate(R.layout.frag_home);
    }

    @Override
    protected void initChildView() {
        mBalanceView = findViewById(R.id.tv_frag_home_balance);
        mGridView = findViewById(R.id.gv_frag_home);

        mLeaderContainner = findViewById(R.id.tv_frag_home_leader_containner);

        if (AppUtils.isLogin()){
            if (AccountDao.getInstance().getAccount().role == 1){
                mLeaderContainner.setVisibility(View.VISIBLE);
            } else {
                mLeaderContainner.setVisibility(View.GONE);
            }
        }

    }

    @Override
    protected void load() {
        mDataList = new ArrayList<>();

        mDataList.add(new HomeOpreratorBean("贷款申请", R.mipmap.loan_apply));
        mDataList.add(new HomeOpreratorBean("信用卡申请", R.mipmap.credit_apply));
        mDataList.add(new HomeOpreratorBean("持续加入中", R.mipmap.more_home_frag_operator));

        mHomeOperatorAdapter = new HomeOperatorAdapter(mContext, mDataList);
        mGridView.setAdapter(mHomeOperatorAdapter);

        getBalanceFromNet();
    }

    @Override
    protected void setChildViewListener() {
        findViewById(R.id.gv_frag_home_ad).setOnClickListener(this);

        findViewById(R.id.rl_frag_home_cash).setOnClickListener(this);
        findViewById(R.id.rl_frag_home_account).setOnClickListener(this);
        findViewById(R.id.tv_frag_home_transaction_history).setOnClickListener(this);

        findViewById(R.id.tv_frag_home_report_manager).setOnClickListener(this);
        findViewById(R.id.tv_frag_home_shop_manager).setOnClickListener(this);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                HomeOpreratorBean homeOpreratorBean = mDataList.get(position);
                Bundle bundle = new Bundle();
                bundle.putString(WebViewActivity.TITLE, homeOpreratorBean.title);
                switch (position){
                    case 0: //贷款申请
                        bundle.putString(WebViewActivity.URL, "https://loan.rongba.com/H5tuiguang/kff?ref=hd_11015964&sid=haoshouyin001");
                        ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                        break;
                    case 1: //信用卡申请
                        bundle.putString(WebViewActivity.URL, "http://channel.haodai.com/Mobile/creditcard/ref/hd_11015964/sid/haoshouyin001");
                        ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                        break;
                    case 2:
                        UIUtils.showToast("敬请期待!");
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.rl_frag_home_account:
                if (AppUtils.isLogin()) {
                    if (AccountDao.getInstance().getAccount().role != 1){
                        ActivityUtils.startActivity(mContext, AccountActivity.class);
                    } else {
                        if (AppUtils.authorStep(mContext, AccountDao.getInstance().getAccount().auStep)) {
                            if (AccountDao.getInstance().getAccount().auStatus == 2) {
                                UIUtils.showToast("请等待审核结果");
                            } else if (AccountDao.getInstance().getAccount().auStatus == 1) {//审核成功
                                ActivityUtils.startActivity(mContext, AccountActivity.class);
                            } else if (AccountDao.getInstance().getAccount().auStatus == 3) {
                                mTipsDialog = new TipsDialog(mContext);
                                mTipsDialog.setContent("是否重新签约信息");
                                mTipsDialog.show();
                                mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

                                    @Override
                                    public void doEnter() {
                                        mTipsDialog.dismiss();
                                        ActivityUtils.startActivity(mContext, IdentificationShopStep1Activity.class);
                                    }

                                    @Override
                                    public void doCancel() {
                                        mTipsDialog.dismiss();
                                    }
                                });
                            }
                        }
                    }
                } else {
                    ActivityUtils.startActivity(mContext, LoginActivity.class);
                }
                break;
            case R.id.tv_frag_home_transaction_history:

                if (AppUtils.isLogin()){
                    if (AccountDao.getInstance().getAccount().role != 1){
                        ActivityUtils.startActivity(mContext, TransactionHistoryActivity.class);
                    } else {
                        if (AppUtils.authorStep(mContext, AccountDao.getInstance().getAccount().auStep)) {
                            if (AccountDao.getInstance().getAccount().auStatus == 2) {
                                UIUtils.showToast("请等待审核结果");
                            } else if (AccountDao.getInstance().getAccount().auStatus == 1) {//审核成功
                                ActivityUtils.startActivity(mContext, TransactionHistoryActivity.class);
                            } else if (AccountDao.getInstance().getAccount().auStatus == 3) {
                                mTipsDialog = new TipsDialog(mContext);
                                mTipsDialog.setContent("是否重新签约信息");
                                mTipsDialog.show();
                                mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

                                    @Override
                                    public void doEnter() {
                                        mTipsDialog.dismiss();
                                        ActivityUtils.startActivity(mContext, IdentificationShopStep1Activity.class);
                                    }

                                    @Override
                                    public void doCancel() {
                                        mTipsDialog.dismiss();
                                    }
                                });
                            }
                        }
                    }
                } else {
                    ActivityUtils.startActivity(mContext, LoginActivity.class);
                }

                break;
            case R.id.tv_frag_home_shop_manager:
                if (AppUtils.isLogin()) {
                    if (AppUtils.authorStep(mContext, AccountDao.getInstance().getAccount().auStep)){
                        if (AccountDao.getInstance().getAccount().auStatus == 2){
                            UIUtils.showToast("请等待审核结果");
                        } else if (AccountDao.getInstance().getAccount().auStatus == 1){//审核成功
                            ActivityUtils.startActivity(mContext, ShopManagerActivity.class);
                        } else if (AccountDao.getInstance().getAccount().auStatus == 3){
                            mTipsDialog = new TipsDialog(mContext);
                            mTipsDialog.setContent("是否重新签约信息");
                            mTipsDialog.show();
                            mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener(){

                                @Override
                                public void doEnter() {
                                    mTipsDialog.dismiss();
                                    ActivityUtils.startActivity(mContext, IdentificationShopStep1Activity.class);
                                }

                                @Override
                                public void doCancel() {
                                    mTipsDialog.dismiss();
                                }
                            });
                        }
                    }
                } else {
                    ActivityUtils.startActivity(mContext, LoginActivity.class);
                }
                break;
            case R.id.rl_frag_home_cash:
            case R.id.tv_frag_home_report_manager:
                UIUtils.showToast("该功能暂无开通，敬请期待!");
                break;
            case R.id.gv_frag_home_ad:
                Bundle bundle = new Bundle();
                bundle.putString(WebViewActivity.TITLE, "帮助");
                bundle.putString(WebViewActivity.URL, "http://hsy.qianbaojiajia.com/help.html");
                ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                break;
        }
    }

    public void getBalanceFromNet(){
        if (AccountDao.getInstance().getAccount().accountID != 0) {
            ApiManager.getApi().getBalance(
                    "HSY001033",
                    AccountDao.getInstance().getAccount().accountID
            ).enqueue(new NetCallBack<BalanceBean>() {
                @Override
                protected void onSuccess(Call<BalanceBean> call, Response<BalanceBean> response) {
                    BalanceBean balanceBean = response.body();
                    if (balanceBean.totalAmount != 0) {
                        mBalanceView.setText("￥ " + NumberUtils.getTwoBit(balanceBean.totalAmount));
                    } else {
                        mBalanceView.setText("￥ 0");
                    }
                }

                @Override
                protected void hiddenProgress() {

                }
            });
        } else {

            mBalanceView.setText("￥ 0");
            if (AccountDao.getInstance().getAccount().id != 0){
                ApiManager.getApi().syncAccoutInfo(
                        "HSY001028",
                        AccountDao.getInstance().getAccount().id
                ).enqueue(new NetCallBack<AccountShopBean>() {
                    @Override
                    protected void onSuccess(Call<AccountShopBean> call, Response<AccountShopBean> response) {
                        AccountShopBean accountShopBean = response.body();

                        AccountDao.getInstance().authorStatus(accountShopBean.appBizShop.status);
                        AccountDao.getInstance().accoutId(accountShopBean.appAuUser.accountID);

                        EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_AUTHOR_SUCCESS));

                        if (accountShopBean.totalAmount != 0) {
                            mBalanceView.setText("￥ " + NumberUtils.getTwoBit(accountShopBean.totalAmount));
                        } else {
                            mBalanceView.setText("￥ 0");
                        }
                    }
                });
            }

        }
    }

    @Override
    protected void onVisible() {
        getBalanceFromNet();
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode){
            case EventCode.EVENT_CODE_BALANCE_CHANGE:
                getBalanceFromNet();
                break;
            case EventCode.EVENT_CODE_START_LOGIN:
                if (AccountDao.getInstance().getAccount().role == 1){
                    mLeaderContainner.setVisibility(View.VISIBLE);
                } else {
                    mLeaderContainner.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        PushManager.getInstance().registerPushIntentService(mContext, GetuiService.class);
    }
}
